using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using LayoutViewer.Models;
using LayoutViewer.Models.Helpers;

namespace LayoutViewer.Views
{
    public partial class Config : Window
    {

        public Config()
        {
            this.CanResize = false;
            this.ExtendClientAreaToDecorationsHint = true;
            this.ExtendClientAreaTitleBarHeightHint = -1;
            this.ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif
        }

        private static string RemoteIP = string.Empty;
        private static string AdbIP = string.Empty;
        private static string AdbPort = string.Empty;


        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
            this.FindControl<TextBox>("LocalIP").Text = Util.GetLocalAddress();
            this.FindControl<TextBox>("RemoteIP").Text = RemoteIP;
            this.FindControl<TextBox>("AdbIP").Text = AdbIP;

        }

        private void DialogResult()
        {
            var cmb = this.FindControl<ComboBox>("ModeSet");

            IHelper? helper = null;

            string? localIP;

            if (cmb.SelectedIndex == 0)
            {
                localIP = this.FindControl<TextBox>("LocalIP").Text.Trim();
                helper = new TcpHelper(localIP);
            }
            else if (cmb.SelectedIndex == 1)
            {
                localIP = this.FindControl<TextBox>("LocalIP").Text.Trim();
                RemoteIP = this.FindControl<TextBox>("RemoteIP").Text.Trim();
                helper = new AJHelper(localIP, RemoteIP);

            }
            else if (cmb.SelectedIndex == 2)
            {
                AdbIP = this.FindControl<TextBox>("AdbIP").Text.Trim();
                AdbPort = this.FindControl<TextBox>("AdbPort").Text ?? string.Empty.Trim();
                helper = new AdbHelper(AdbIP, AdbPort);
            }

            Close(helper);
        }

        private void OK_Tapped(object sender, RoutedEventArgs e)
        {
            DialogResult();
        }

        private void ModeSet_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cmb = sender as ComboBox;
            if (cmb != null)
            {
                if (cmb.SelectedIndex == 0)
                {
                    this.FindControl<StackPanel>("LocalIPLayout").IsVisible = true;
                    this.FindControl<StackPanel>("RemoteIPLayout").IsVisible = false;
                    this.FindControl<StackPanel>("AdbIPLayout").IsVisible = false;
                    this.Height = 270;
                }
                else if (cmb.SelectedIndex == 1)
                {
                    this.FindControl<StackPanel>("LocalIPLayout").IsVisible = true;
                    this.FindControl<StackPanel>("RemoteIPLayout").IsVisible = true;
                    this.FindControl<StackPanel>("AdbIPLayout").IsVisible = false;
                    this.Height = 335;
                }
                else if (cmb.SelectedIndex == 2)
                {
                    this.FindControl<StackPanel>("LocalIPLayout").IsVisible = false;
                    this.FindControl<StackPanel>("RemoteIPLayout").IsVisible = false;
                    this.FindControl<StackPanel>("AdbIPLayout").IsVisible = true;
                    this.Height = 335;
                }
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            var key = e.Key;
            switch (key)
            {
                case Key.Enter:
                    DialogResult(); break;

                case Key.Escape: Close(); break;

                default: return;
            }
            e.Handled = true;
        }
    }
}
